<?php

    namespace App\Http\Controllers\V1_bak;

    use App\Dictionary\ErrorCode;
    use App\Http\Controllers\Controller;
    use App\Models\V1_bak\Goods;
    use Dingo\Api\Routing\Helpers;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;

    class GoodsController extends Controller
    {
        use Helpers;

        /**
         * 获取商品列表
         *
         * @param Request $request
         *
         * @return mixed
         * author: walker
         * Date: 2019/10/22
         * Time: 18:10
         */
        public function getGoodsList(Request $request)
        {

            $params = $request->all();

            $currentPage = $params['current_page'] ?: 1;
            $pageNum     = $params['page_num'] ?: 10;
            $startPage   = ($currentPage - 1) * $pageNum;
            $isNew       = !array_key_exists('IsNew', $params) ? 0 : $params['IsNew'];
            $isHot       = !array_key_exists('IsNew', $params) ? 0 : $params['IsHot'];
            $isIndex     = !array_key_exists('IsNew', $params) ? 0 : $params['IsIndex'];
            $sortBy      = !array_key_exists('IsNew', $params) ? 0 : $params['SortBy'];
            $sortType    = !array_key_exists('IsNew', $params) ? 0 : $params['SortType'];

            $model = new Goods();
            $where = [];
            /**
             * 搜索条件
             */
            if ( !empty($isNew)){
                $where['IsNew'] = 1;
            }
            if ( !empty($isHot)){
                $where['IsHot'] = 1;
            }
            if ( !empty($isIndex)){
                $where['IsIndex'] = 1;
            }
            /**
             * 排序
             */
            if (isset($sortBy)){
                $sortBy = '';
                switch ((int)$sortBy) {
                    case 1:
                        $sortBy = 'TotalRating';
                        break;
                    case 2:
                        $sortBy = 'Rating';
                        break;
                    case 3:
                        $sortBy = 'Price_1';
                        break;
                    case 4:
                        $sortBy = 'Sales';
                        break;
                    default:
                        $sortBy = 'MyOrder';
                        break;

                }
                $sortType = $sortType == 1 ? 'asc' : 'desc';
            }

            $goodsLists = $model->getGoodsLists($sortBy, $sortType, $startPage, $pageNum, $where);
            $field      = 'Name_en,CateId,ProId';
            if (empty($goodsLists)){
                ajaxReturn(4001, ErrorCode::$common[4001]);
            }
            ajaxReturn(200, ErrorCode::$common[200], $goodsLists);
        }

        /**
         * 修改商品名 可删
         * @param Request $request
         * author: walker
         * Date: 2019/11/9
         * Time: 9:40
         * Note:
         */
        public function editGoodsName(Request $request)
        {
            $table = 'article_content';
            $field = 'CId,Content_en';
            $goodsList = DB::table($table)->selectRaw($field)->get();
            $goodsList = toArr($goodsList);
            $data      = [];

            foreach ($goodsList as $key => $val) {
                $CId      = $val['CId'];
                $name = str_replace('LAURADRESS', 'THECHERIS', $val['Content_en']);
                $res = DB::table($table)->where('CId',$CId)->update(['Content_en'=>$name]);
            }

            ajaxReturn(200, '修改成功');
        }
    }
